package
{
   import _debug.Stats;
   
   import com.bit101.components.CheckBox;
   import com.bit101.components.ComboBox;
   import com.bit101.components.HUISlider;
   
   import flash.display.BlendMode;
   import flash.display.MovieClip;
   import flash.display.StageAlign;
   import flash.display.StageScaleMode;
   import flash.events.Event;
   import flash.events.KeyboardEvent;
   import flash.events.MouseEvent;
   import flash.ui.Keyboard;
   
   public class LightEffect_1_Main extends MovieClip
   {
      // fla
      public var mcLight:MovieClip;
      
      // ui
      private var comBlendMode:ComboBox;
      private var comSize:HUISlider;
      private var comAlpha:HUISlider;
      private var comCachBmp:CheckBox;
      
      // flag
      private var alphaMode:Boolean = false;
      
      public function LightEffect_1_Main()
      {
         super();
         
         addEventListener(Event.ADDED_TO_STAGE, onAdd);
         addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
      }
      
      // --------------------- LINE ---------------------
      
      // ################### protected ##################
      
      // #################### private ###################
      
      private function onAdd(e:Event):void
      {
         // basic
         stage.align = StageAlign.TOP_LEFT;
         stage.scaleMode = StageScaleMode.NO_SCALE;
         
         init();
         initUI();
         
         // benchmark
         var stat:Stats = new Stats(); // 70x100
         stat.x = 506;
         stat.y = 0;
         addChild(stat);
      }
      
      private function onRemove(e:Event):void
      {
      }
      
      // ________________________________________________
      //                                             init
      
      private function init():void
      {
         addEventListener(Event.ENTER_FRAME, onMMove);
      }
      
      private function initUI():void
      {
         comBlendMode = new ComboBox(this, 405, 23 + 140, '', [BlendMode.NORMAL, BlendMode.ADD, BlendMode.SCREEN, BlendMode.SUBTRACT]);
         comBlendMode.selectedIndex = 1;
         comBlendMode.setSize(80, 20);
         comBlendMode.addEventListener(Event.SELECT, onBlendModeChange);
         
         comSize = new HUISlider(this, 398, 75 + 140, '', onSizeChange);
         comSize.minimum = 0;
         comSize.maximum = 5;
         comSize.tick = 0.01;
         comSize.value = 1;
         comSize.labelPrecision = 2;
         
         comAlpha = new HUISlider(this, 398, 128 + 140, '', onAlphaChange);
         comAlpha.minimum = 0;
         comAlpha.maximum = 1;
         comAlpha.tick = 0.01;
         comAlpha.value = 1;
         comAlpha.labelPrecision = 2;
         
         comCachBmp = new CheckBox(this, 500, 307, '', onRenderChange);
         comCachBmp.selected = mcLight.cacheAsBitmap;
      }
      
      // --------------------- LINE ---------------------
      
      private function onMMove(e:Event):void
      {
         var xx = mouseX;
         var yy = mouseY;
         
         if (xx < 0) xx = 0;
         else if (xx > 400) xx = 400;
         
         if (yy < 0) yy = 0;
         else if (yy > 340) yy = 340;
         
         mcLight.x = xx;
         mcLight.y = yy;
      }
      
      private function onBlendModeChange(e:Event):void
      {
         mcLight.blendMode = String(comBlendMode.selectedItem);
      }
      
      private function onSizeChange(e:Event):void
      {
         updateSize(comSize.value);
      }
      
      private function updateSize(v:Number):void
      {
         mcLight.scaleX = mcLight.scaleY = comSize.value = v;
      }
      
      private function onAlphaChange(e:Event):void
      {
         updateAlpha(comAlpha.value);
      }
      
      private function updateAlpha(v:Number):void
      {
         mcLight.alpha = comAlpha.value = v;
      }
      
      private function onRenderChange(e:Event):void
      {
         mcLight.cacheAsBitmap = comCachBmp.selected;
      }
      
      // --------------------- LINE ---------------------
      
   }
   
}